<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Tuolaji <479923197@qq.com>
// +----------------------------------------------------------------------
/**
 */
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
header('Content-type:text/html;charset=utf-8');


class ReportController extends AdminbaseController
{


    //代理管理列表
    public function geren_baobiao(){

    $data = M('Accountsinfo')->field("userid,accounts")->select();
    foreach($data as $v){
        $uid[] = $v['userid'];
    }
    $uids = implode(",",$uid);
    if($uids){
        $touzhu = M("jiaoyi")->where("type=3 and uid in($uids)")->select();
        $chongzhi = M("jiaoyi")->where("type=1 and uid in($uids)")->select();
        $tixian = M("jiaoyi")->where("type=2 and uid in($uids)")->select();
        $zhongjiang = M("jiaoyi")->where("type=5 and uid in($uids)")->select();
        $huodong = M("jiaoyi")->where("type=7 and uid in($uids)")->select();
        $fandian = M("jiaoyi")->where("type=4 and uid in($uids)")->select();
    }

    foreach($data as $k=>$v){
        foreach($touzhu as $kk=>$vv){
            if($v['userid'] == $vv['uid']){
                $data[$k]['touzhu'] += $vv["zhi_money"];
            }
        }
    }
    foreach($data as $k=>$v){
        foreach($chongzhi as $kk=>$vv){
            if($v['userid'] == $vv['uid']){
                $data[$k]['chongzhi'] += $vv["shou_money"];
            }
        }
    }
    foreach($data as $k=>$v){
        foreach($tixian as $kk=>$vv){
            if($v['userid'] == $vv['uid']){
                $data[$k]['tixian'] += $vv["zhi_money"];
            }
        }
    }
    foreach($data as $k=>$v){
        foreach($zhongjiang as $kk=>$vv){
            if($v['userid'] == $vv['uid']){
                $data[$k]['zhongjiang'] += $vv["shou_money"];
            }
        }
    }
    foreach($data as $k=>$v){
        foreach($huodong as $kk=>$vv){
            if($v['userid'] == $vv['uid']){
                $data[$k]['huodong'] += $vv["shou_money"];
            }
        }
    }
    foreach($data as $k=>$v){
        foreach($fandian as $kk=>$vv){
            if($v['userid'] == $vv['uid']){
                $data[$k]['fandian'] += $vv["shou_money"];
            }
        }
    }

   $this->assign("arr",$data);
     $this->display();
}

    public function zong_baobiao(){
        $kaishi = I("kaishi");
        $jieshu = I("jieshu");


        $where1 = array();
        //时间搜索
        if(!empty($kaishi) && !empty($jieshu)){
            $start = strtotime($kaishi);
            $end = strtotime($jieshu);
            $where1['ctime'] = array(array('EGT',$start),array('ELT',$end));
        }
        $where1['type']=3;
        $touzhu = count(M("jiaoyi")->where($where1)->select());


        $where2 = array();
        //时间搜索
        if(!empty($kaishi) && !empty($jieshu)){
            $start = strtotime($kaishi);
            $end = strtotime($jieshu);
            $where2['ctime'] = array(array('EGT',$start),array('ELT',$end));
        }
        $where2['type']=5;
        $youxiao = count(M("jiaoyi")->where($where2)->select());



        $where3 = array();
        //时间搜索
        if(!empty($kaishi) && !empty($jieshu)){
            $start = strtotime($kaishi);
            $end = strtotime($jieshu);
            $where3['ctime'] = array(array('EGT',$start),array('ELT',$end));
        }
        $where3['type']=1;
        $dat1 = M("jiaoyi")->where($where3)->select();
        if($dat1){
            $cunkuan = 0;
            foreach($dat1 as $v){
               $cunkuan += $v['shou_money'];
            }
        }

        $where4 = array();
        //时间搜索
        if(!empty($kaishi) && !empty($jieshu)){
            $start = strtotime($kaishi);
            $end = strtotime($jieshu);
            $where4['ctime'] = array(array('EGT',$start),array('ELT',$end));
        }else{
            $start = strtotime($kaishi."00:00:00");
            $end = strtotime($jieshu."23:59:59");
            $where4['ctime'] = array(array('EGT',$start),array('ELT',$end));
        }


        $zhuce = count(M('Accountsinfo')->where($where4)->select());
        $where4['type']=1;
        $dd = M("jiaoyi")->where($where4)->field("uid")->distinct(true)->select();
        $chongzhi =  count($dd);


        //时间搜索
        $where5 = array();
        if(!empty($kaishi) && !empty($jieshu)){
            $start = strtotime($kaishi);
            $end = strtotime($jieshu);
            $where5['ctime'] = array(array('EGT',$start),array('ELT',$end));
        }

//        投注人数
        $where6 = array();
        //时间搜索
        if(!empty($kaishi) && !empty($jieshu)){
            $start = strtotime($kaishi);
            $end = strtotime($jieshu);
            $where6['ctime'] = array(array('EGT',$start),array('ELT',$end));
        }
        $where6['type']=3;
        $touzhurenshu =count(M("jiaoyi")->where($where6)->field("uid")->distinct(true)->select());
        $this->assign("touzhurenshu",$touzhurenshu);

  //返点总额
        $where7 = array();
        //时间搜索
        if(!empty($kaishi) && !empty($jieshu)){
            $start = strtotime($kaishi);
            $end = strtotime($jieshu);
            $where7['ctime'] = array(array('EGT',$start),array('ELT',$end));
        }
        $where7['type']=4;
        $dat2 = M("jiaoyi")->where($where7)->select();
        if($dat2){
            $fandian = 0;
            foreach($dat2 as $v){
                $fandian += $v['shou_money'];
            }
        }
        $this->assign("fandian",$fandian);

        //中奖总额
        $where8 = array();
        //时间搜索
        if(!empty($kaishi) && !empty($jieshu)){
            $start = strtotime($kaishi);
            $end = strtotime($jieshu);
            $where8['ctime'] = array(array('EGT',$start),array('ELT',$end));
        }
        $where8['type']=5;
        $dat3 = M("jiaoyi")->where($where8)->select();
        if($dat3){
            $zhongjiang = 0;
            foreach($dat3 as $v){
                $zhongjiang += $v['shou_money'];
            }
        }
        $this->assign("zhongjiang",$zhongjiang);

        //活动礼金总额
        $where9 = array();
        //时间搜索
        if(!empty($kaishi) && !empty($jieshu)){
            $start = strtotime($kaishi);
            $end = strtotime($jieshu);
            $where9['ctime'] = array(array('EGT',$start),array('ELT',$end));
        }
        $where9['type']=7;
        $dat4 = M("jiaoyi")->where($where9)->select();
        if($dat4){
            $huodong = 0;
            foreach($dat4 as $v){
                $huodong += $v['shou_money'];
            }
        }
        $this->assign("huodong",$huodong);

        //提现总额
        $where10 = array();
        //时间搜索
        if(!empty($kaishi) && !empty($jieshu)){
            $start = strtotime($kaishi);
            $end = strtotime($jieshu);
            $where10['ctime'] = array(array('EGT',$start),array('ELT',$end));
        }
        $where10['type']=2;
        $dat5 = M("jiaoyi")->where($where10)->select();
        if($dat5){
            $tixian = 0;
            foreach($dat5 as $v){
                $tixian += $v['zhi_money'];
            }
        }
        $this->assign("tixian",$tixian);
//投注总额
        $where11 = array();
        //时间搜索
        if(!empty($kaishi) && !empty($jieshu)){
            $start = strtotime($kaishi);
            $end = strtotime($jieshu);
            $where11['ctime'] = array(array('EGT',$start),array('ELT',$end));
        }
        $where11['type']=3;
        $dat6 = M("jiaoyi")->where($where11)->select();
        if($dat6){
            $touzhujine = 0;
            foreach($dat6 as $v){
                $touzhujine += $v['zhi_money'];
            }
        }
        $this->assign("touzhujine",$touzhujine);
  //盈亏
        $yingkuizonge = $zhongjiang-$touzhujine+$huodong+$fandian;
        $this->assign("yingkuizonge",$yingkuizonge);

 //账号余额
        $ddd = M('Accountsinfo')->select();
        $zhanghu_yue = 0;
        foreach($ddd as $v){
           $zhanghu_yue += $v['money'];
        }
        $this->assign("zhanghu_yue",$zhanghu_yue);




        //首冲人数
        function FetchRepeatMemberInArray($array) {
            // 获取去掉重复数据的数组
            $unique_arr = array_unique ( $array );
            // 获取重复数据的数组
            $repeat_arr = array_diff_assoc ( $array, $unique_arr );
            return $repeat_arr;
        } //获取数组中重复的数据 (这是一个封装的函数)
        $data = M('Accountsinfo')->field("userid,accounts")->select();
        foreach($data as $v){
            $uid[] = $v['userid'];
        }
        $uids = implode(",",$uid);
        $where5['uid'] = array("IN",$uids);
        $where5['type'] = 1;
        if($uids){
            $jiaoyi_sc = M("jiaoyi")->where($where5)->field("uid")->select();
        }

        foreach($jiaoyi_sc as $v){
            $cc[] = $v['uid'];
        }

        if($uids) {
            $meiyou_cz = M("Accountsinfo")->where("userid in ($uids) and score=0")->field("userid")->select();
        }

        foreach($meiyou_cz as $v){
            $meiyou_cz2[] = $v['userid'];
        }

        $chongfu_uid = FetchRepeatMemberInArray($cc);//有重复的UID

        if($chongfu_uid){
            $uidss = explode(",",$uids);//把原先获取到的UID字符串变为一个数组

            $count1_sc = array_diff($uidss,$chongfu_uid);//求两个数组的差集  array_diff()
//            dump($count1_sc);die;
            $count_sc = count(array_diff($count1_sc,$meiyou_cz2));

        }


        $this->assign("count_sc",$count_sc);
        $this->assign("chongzhi",$chongzhi);
        $this->assign("zhuce",$zhuce);
        $this->assign("kaishi",$kaishi);
        $this->assign("jieshu",$jieshu);
        $this->assign("cunkuan",$cunkuan);
        $this->assign("youxiao",$youxiao);
        $this->assign("touzhu",$touzhu);
        $this->display();
    }


//个人投注
 public function shuju_tz(){
     $uid = I("id");
     $p = I("p");
     $where = array();
     $where['uid'] = $uid;

     //搜索彩票名称
     $classs = M('lottery_class')->select();
     //列表
     $data1 = M('lottery_add')->where($where)->select();
     $data2 = M('lottery_11x5')->where($where)->select();
     $data3 = M('lottery_fc3d')->where($where)->select();
     $data4 = M('lottery_kl8')->where($where) ->select();
     $data5 = M('lottery_pk10')->where($where)->select();
     $data6 = M('lottery_ssc')->where($where)->select();
     foreach($data1 as $k=>$v){
         $data1[$k]['table'] = 1;
     }
     foreach($data2 as $k=>$v){
         $data2[$k]['table'] = 2;
     }
     foreach($data3 as $k=>$v){
         $data3[$k]['table'] = 3;
     }
     foreach($data4 as $k=>$v){
         $data4[$k]['table'] = 4;
     }
     foreach($data5 as $k=>$v){
         $data5[$k]['table'] = 5;
     }
     foreach($data6 as $k=>$v){
         $data6[$k]['table'] = 6;
     }
     $data = array_merge($data1,$data2,$data3,$data4,$data5,$data6);
     foreach ($data as $user) {
         $ages[] = $user['ctime'];
     }
     array_multisort($ages, SORT_DESC, $data);


//列表
     $data11 = M('lottery_add')->where($where)->select();
     $data22 = M('lottery_11x5')->where($where)->select();
     $data33 = M('lottery_fc3d')->where($where)->select();
     $data44 = M('lottery_kl8')->where($where) ->select();
     $data55 = M('lottery_pk10')->where($where)->select();
     $data66 = M('lottery_ssc')->where($where)->select();
     foreach($data11 as $k=>$v){
         $data11[$k]['table'] = 1;
     }
     foreach($data22 as $k=>$v){
         $data22[$k]['table'] = 2;
     }
     foreach($data33 as $k=>$v){
         $data33[$k]['table'] = 3;
     }
     foreach($data44 as $k=>$v){
         $data44[$k]['table'] = 4;
     }
     foreach($data55 as $k=>$v){
         $data55[$k]['table'] = 5;
     }
     foreach($data66 as $k=>$v){
         $data66[$k]['table'] = 6;
     }
     $data00 = array_merge($data11,$data22,$data33,$data44,$data55,$data66);
     foreach ($data00 as $user) {
         $ages0[] = $user['ctime'];
     }
     array_multisort($ages0, SORT_DESC, $data00);

//分页 begion
     $count = count($data);
     if($count==0){
         $p = 0;
     }else{
         if(empty($p)){
             $p = 1;
         }
     }
     $data = array_slice($data,($p-1)*10,10);
     $totalpage = ceil($count/10);
     $page = $this->page($count,10);
     //end

     $touzhu = 0;   //投注钱数
     $zhongjiang = 0; //中奖钱数
     $zhongjiang2 = 0; //中奖钱数
     $yingli = 0;//盈利钱数
     foreach($data00 as $v){
         $touzhu += $v["money"];
         $zhongjiang2 += $v["zong_money"];
         $yingli = $zhongjiang2-$touzhu;
     }

     foreach($data as $v){
         $zhongjiang += $v["zong_money"];
     }

     foreach($data as $v){
         $lotteryclass[] = $v["lotteryclass"]; //彩种
         $ids[] = $v["id"];  //内容
     }

     //彩种
     $lotteryclasss = implode(",",$lotteryclass);
     if($lotteryclasss){
         $class = M('lottery_class')->where("id in($lotteryclasss)")->select();
     }

     //内容
     $idss = implode(",",$ids);

     if($idss){
         $lottery_add_detail  = M('lottery_add_detail')->where("lottery_add_id in($idss)")->field("lottery_add_id,cal,is_zhong")->select();
         $lottery_11x5_detail = M('lottery_11x5_detail')->where("lottery_11x5_id in($idss)")->field("lottery_11x5_id,cal,is_zhong")->select();
         $lottery_fc3d_detail = M('lottery_fc3d_detail')->where("lottery_fc3d_id in($idss)")->field("lottery_fc3d_id,cal,is_zhong")->select();
         $lottery_kl8_detail = M('lottery_kl8_detail')->where("lottery_kl8_id in($idss)")->field("lottery_kl8_id,cal,is_zhong")->select();
         $lottery_pk10_detail = M('lottery_pk10_detail')->where("lottery_pk10_id in($idss)")->field("lottery_pk10_id,cal,is_zhong")->select();
         $lottery_ssc_detail = M('lottery_ssc_detail')->where("lottery_ssc_id in($idss)")->field("lottery_ssc_id,cal,is_zhong")->select();
     }

     $a1 = array();
     foreach($lottery_add_detail as $v){
         //内容
         if(isset($a1[$v["lottery_add_id"]])){
             $a1[$v["lottery_add_id"]] = $a1[$v["lottery_add_id"]].'#'.$v["cal"];
         }else{
             $a1[$v["lottery_add_id"]] = $v["cal"];
         }
     }

     $a2 = array();
     foreach($lottery_11x5_detail as $v){
         //内容
         if(isset($a2[$v["lottery_11x5_id"]])){
             $a2[$v["lottery_11x5_id"]] = $a2[$v["lottery_11x5_id"]].'#'.$v["cal"];
         }else{
             $a2[$v["lottery_11x5_id"]] = $v["cal"];
         }
     }
     $a3 = array();
     foreach($lottery_fc3d_detail as $v){
         //内容
         if(isset($a3[$v["lottery_fc3d_id"]])){
             $a3[$v["lottery_fc3d_id"]] = $a3[$v["lottery_fc3d_id"]].'#'.$v["cal"];
         }else{
             $a3[$v["lottery_fc3d_id"]] = $v["cal"];
         }
     }
     $a4 = array();
     foreach($lottery_kl8_detail as $v){
         //内容
         if(isset($a4[$v["lottery_kl8_id"]])){
             $a4[$v["lottery_kl8_id"]] = $a4[$v["lottery_kl8_id"]].'#'.$v["cal"];
         }else{
             $a4[$v["lottery_kl8_id"]] = $v["cal"];
         }
     }
     $a5 = array();
     foreach($lottery_pk10_detail as $v){
         //内容
         if(isset($a5[$v["lottery_pk10_id"]])){
             $a5[$v["lottery_pk10_id"]] = $a5[$v["lottery_pk10_id"]].'#'.$v["cal"];
         }else{
             $a5[$v["lottery_pk10_id"]] = $v["cal"];
         }
     }
     $a6 = array();
     foreach($lottery_ssc_detail as $v){
         //内容
         if(isset($a6[$v["lottery_ssc_id"]])){
             $a6[$v["lottery_ssc_id"]] = $a6[$v["lottery_ssc_id"]].'#'.$v["cal"];
         }else{
             $a6[$v["lottery_ssc_id"]] = $v["cal"];
         }
     }


     foreach($data as $k=>$v){
         foreach($class as $kk=>$vv){
             if($v["lotteryclass"] == $vv["id"]){
                 $data[$k]["classname"] = $vv["name"];
             }
         }
     }

     $this->assign("zhongjiang",$zhongjiang);
     $this->assign("yingli",$yingli);
     $this->assign("touzhu",$touzhu);
     $this->assign("count",$count);
     $this->assign("a1",$a1);
     $this->assign("a2",$a2);
     $this->assign("a3",$a3);
     $this->assign("a4",$a4);
     $this->assign("a5",$a5);
     $this->assign("a6",$a6);
     $this->assign("arr",$data);
     $this->assign("p",$p);
     $this->assign("totalpage",$totalpage);
     $this -> assign("page",$page->show('Admin'));
     $this->assign("classs",$classs);
     $this->assign("uid",$uid);
     $this->display();
 }

//个人充值
public function shuju_cz(){
    $uid = I("id");
    $p = I("p");
    $data = M("jiaoyi")->where("uid = $uid and type=1")->select();

    //分页 begion
    $count = count($data);
    if($count==0){
        $p = 0;
    }else{
        if(empty($p)){
            $p = 1;
        }
    }
    $data = array_slice($data,($p-1)*10,10);
    $totalpage = ceil($count/10);
    $page = $this->page($count,10);
    //end


    $this->assign("arr",$data);
    $this->assign("p",$p);
    $this->assign("totalpage",$totalpage);
    $this -> assign("page",$page->show('Admin'));
    $this->assign("uid",$uid);
    $this->display();
}
//个人提现
    public function shuju_tx(){
        $uid = I("id");
        $p = I("p");
        $data = M("jiaoyi")->where("uid = $uid and type=2")->select();

        //分页 begion
        $count = count($data);
        if($count==0){
            $p = 0;
        }else{
            if(empty($p)){
                $p = 1;
            }
        }
        $data = array_slice($data,($p-1)*10,10);
        $totalpage = ceil($count/10);
        $page = $this->page($count,10);
        //end


        $this->assign("arr",$data);
        $this->assign("p",$p);
        $this->assign("totalpage",$totalpage);
        $this -> assign("page",$page->show('Admin'));
        $this->assign("uid",$uid);
        $this->display();
    }

//个人交易
    public function shuju_jy(){
        $uid = I("id");
        $p = I("p");
        $data = M("jiaoyi")->where("uid = $uid")->select();

        //分页 begion
        $count = count($data);
        if($count==0){
            $p = 0;
        }else{
            if(empty($p)){
                $p = 1;
            }
        }
        $data = array_slice($data,($p-1)*10,10);
        $totalpage = ceil($count/10);
        $page = $this->page($count,10);
        //end


        $this->assign("arr",$data);
        $this->assign("p",$p);
        $this->assign("totalpage",$totalpage);
        $this -> assign("page",$page->show('Admin'));
        $this->assign("uid",$uid);
        $this->display();
    }





















}